package cn.stylefeng.guns.sys.modular.tao.service.serviceImpl;

import cn.stylefeng.guns.sys.modular.api.sdk.Utils;
import cn.stylefeng.guns.sys.modular.tao.entity.Address;
import cn.stylefeng.guns.sys.modular.tao.mapper.AddressMapper;
import cn.stylefeng.guns.sys.modular.tao.service.AddressService;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdressServiceImpl implements AddressService {

    @Autowired
    private AddressMapper addressMapper;

    /**
     * 添加地址
     *
     * @param address
     * @return
     */
    @Override
    public ResponseData addAddress(Address address) {
        ResponseData data = new ResponseData();
        try {
            if (Utils.checkStringIsNull(address.getIsDefault())) {
                address.setIsDefault("0");
            }
            addressMapper.addAddress(address);
            data.setMessage("添加成功");
            data.setCode(200);
        } catch (Exception e) {
            data.setMessage("添加失败");
            data.setCode(500);
        }
        return data;
    }

    /**
     * 查询地址
     *
     * @param address
     * @return
     */
    @Override
    public ResponseData getAddress(Address address) {
        ResponseData data = new ResponseData();
        try {
            List<Address> addressList = addressMapper.getAddress(address.getUserId());
            data.setCode(200);
            data.setMessage("查询成功");
            data.setData(addressList);
        } catch (Exception e) {
            data.setCode(500);
            data.setMessage("查询失败");
        }
        return data;
    }

    /**
     * 修改地址
     *
     * @param address
     * @return
     */
    @Override
    public ResponseData editAdress(Address address) {
        ResponseData data = new ResponseData();
        try {
            //查询地址是否存在
            Address addressInfo = addressMapper.getAddressById(address.getId());
            if (addressInfo != null) {
                addressMapper.editAdress(address);
                data.setMessage("修改成功");
                data.setCode(200);
            } else {
                data.setMessage("数据不存在");
                data.setCode(500);
            }
        } catch (Exception e) {
            data.setMessage("修改失败");
            data.setCode(500);
        }
        return null;
    }

    /**
     * 修改是否默认
     *
     * @param address
     * @return
     */
    @Override
    public ResponseData editDefault(Address address) {
        ResponseData data = new ResponseData();
        try {
            //查询数据是否存在
            Address addressInfo = addressMapper.getAddressById(address.getId());
            if (addressInfo != null) {
                if ("1".equals(addressInfo.getIsDefault())) {
                    //修改该数据为默认
                    addressMapper.editDefault();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
